package com.aiti.lulian.scheduled;

import com.aiti.lulian.entity.newCard.CardBaseInfo;
import com.aiti.lulian.service.newCard.CardPersonalBaseInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SynchronizeAccessData {
    protected final Logger logger = LoggerFactory.getLogger(SynchronizeAccessData.class);
    private final CardPersonalBaseInfoService cardPersonalBaseInfoService;

    public SynchronizeAccessData(CardPersonalBaseInfoService cardPersonalBaseInfoService) {
        this.cardPersonalBaseInfoService = cardPersonalBaseInfoService;
    }

//    @Scheduled(cron = "0 */5 * * * ? ")
    @Scheduled(cron = "0 59 23 * * ? ")
//    @Scheduled(cron = "0 0 0/1 * * ? ")
    public void syncToDayAccessCount(){
        logger.debug("执行今日新增数据置零");
        List<CardBaseInfo> accessRecords = cardPersonalBaseInfoService.list(null);
        if(accessRecords != null && !accessRecords.isEmpty()) {
            List<CardBaseInfo> list = new ArrayList<>(accessRecords.size());
            for (CardBaseInfo item : accessRecords) {
                Long toDayCount = item.getToDayCount();
//                Long totalCount = item.getViewCount();
                item.setYesterdayCount(toDayCount);
//                item.setViewCount(totalCount + toDayCount);
                item.setToDayCount(0L);
                list.add(item);

                if(list.size() >= 100){
                    cardPersonalBaseInfoService.saveOrUpdateBatch(list);
                    list.clear();
                }
            }
            if(!list.isEmpty()){
                cardPersonalBaseInfoService.saveOrUpdateBatch(list);
            }
        }
    }
}
